package com.daoeping.action;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
public class PindexAction extends ActionSupport{

	private static final long serialVersionUID = -2064945031531051188L;
	private File file;
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public void upload(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd_HHmmss");
		Random r=new Random();
		String path=ServletActionContext.getServletContext().getRealPath("/");
		String imgName=sdf.format(new Date())+r.nextInt(100)+".jpg";
		try {
			FileUtils.copyFile(file,new File(path+"upload\\"+imgName));
			String outPath=ServletActionContext.getRequest().getScheme()+"://"+ServletActionContext.getRequest().getServerName()+":"+ServletActionContext.getRequest().getServerPort()+ServletActionContext.getRequest().getContextPath()+"/";
			ServletActionContext.getResponse().getWriter().print(outPath+"upload/"+imgName);
			System.out.println(outPath+"upload/"+imgName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
}
